/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Esnipe2Gui.java
 *
 * Created on Dec 17, 2011, 3:49:00 PM
 */

package esnipe2;

import java.awt.CardLayout;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author zxyang
 */
public class Esnipe2Gui extends javax.swing.JFrame implements UpdateGUI{

    private double _maxPrice;
    private JFrame frame;//the frame that is used to show message dialog.
    EbayAPI ebayAPI;
    String currURL;
    String currID;
    String currPrice;
    String currStatus;
    String currEndTime;
    String currLastTime;
    String currName;
    String currMaxPrice;
    EbayItem currItem;
    ItemRunnable currRunnable;
    Vector<String> IDs;
    Vector<ItemRunnable> items;
    Vector<String> URLs;
    /** Creates new form Esnipe2Gui */
    public Esnipe2Gui() {
        initComponents();
        cardLayout = (CardLayout)getContentPane().getLayout();
        initTable();
        initCurrInfo();
        IDs = new Vector<String>();
        try {ebayAPI = new EbayAPIImpl();}
        catch(Exception e) {}
        items = new Vector<ItemRunnable>();
        URLs = new Vector<String>();
        
    }

    public void initTable(){
        model.addColumn("URL");
        model.addColumn("ID");
        model.addColumn("Name");
        model.addColumn("Current Price");
        model.addColumn("Maximum Price");
        model.addColumn("Status");
        model.addColumn("End Time");
        model.addColumn("Last Time");
    }

    public void initCurrInfo(){
        currURL = "NULL";
        currID = "NULL" ;
        currPrice = "NULL";
        currStatus = "NULL";
        currEndTime = "NULL";
        currLastTime = "NULL";
        currName = "NULL";
        currMaxPrice = "NULL";
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        singleItemDisplayPanel = new javax.swing.JPanel();
        startSingleButton = new javax.swing.JButton();
        suspendSingleItem = new javax.swing.JButton();
        closeSingleItem = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        currPriceField = new javax.swing.JTextField();
        statusField = new javax.swing.JTextField();
        endTimeField = new javax.swing.JTextField();
        lastTimeField = new javax.swing.JTextField();
        URLSingleField = new javax.swing.JTextField();
        itemNameField = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        maxPrice = new javax.swing.JTextField();
        jOptionPane1 = new javax.swing.JOptionPane();
        SignInPanel = new javax.swing.JPanel();
        signInButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        userPassLabel = new javax.swing.JLabel();
        userIDLabel = new javax.swing.JLabel();
        userIDField = new javax.swing.JTextField();
        userPassField = new javax.swing.JPasswordField();
        biddingTabPanel = new javax.swing.JTabbedPane();
        biddingPanel = new javax.swing.JPanel();
        inputPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        URLField = new javax.swing.JTextField();
        addItemButton = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        maxPriceField = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        closeTimeField = new javax.swing.JTextField();
        start1Button = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        model = new DefaultTableModel();
        displayTable = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int colIntex) {
                return false;
            }

        };
        jPanel1 = new javax.swing.JPanel();
        closeSingleButton = new javax.swing.JButton();
        resumeSingleButton = new javax.swing.JButton();
        suspendSingleButton = new javax.swing.JButton();
        retrySingleButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        SuspendAllButton = new javax.swing.JButton();
        closeAllButton = new javax.swing.JButton();
        ResumeAllButton = new javax.swing.JButton();
        quitButton = new javax.swing.JButton();

        startSingleButton.setText("jButton2");

        suspendSingleItem.setText("jButton3");

        closeSingleItem.setText("jButton5");

        jLabel6.setText("URL");

        jLabel7.setText("Name");

        jLabel8.setText("Current Price");

        jLabel9.setText("Status");

        jLabel10.setText("End Time");

        jLabel11.setText("Last Time");

        jLabel12.setText("Max Price:");

        javax.swing.GroupLayout singleItemDisplayPanelLayout = new javax.swing.GroupLayout(singleItemDisplayPanel);
        singleItemDisplayPanel.setLayout(singleItemDisplayPanelLayout);
        singleItemDisplayPanelLayout.setHorizontalGroup(
            singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(singleItemDisplayPanelLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addGroup(singleItemDisplayPanelLayout.createSequentialGroup()
                        .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(startSingleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(singleItemDisplayPanelLayout.createSequentialGroup()
                                .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(URLSingleField, javax.swing.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
                            .addComponent(itemNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 675, Short.MAX_VALUE)
                            .addGroup(singleItemDisplayPanelLayout.createSequentialGroup()
                                .addComponent(currPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(23, 23, 23)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(maxPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(statusField, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)
                                .addComponent(endTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addComponent(jLabel11)
                                .addGap(18, 18, 18)
                                .addComponent(lastTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(singleItemDisplayPanelLayout.createSequentialGroup()
                                .addGap(39, 39, 39)
                                .addComponent(suspendSingleItem, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                                .addComponent(closeSingleItem, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(521, 521, 521)))))
                .addContainerGap())
        );

        singleItemDisplayPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {currPriceField, endTimeField, lastTimeField, statusField});

        singleItemDisplayPanelLayout.setVerticalGroup(
            singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(singleItemDisplayPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(URLSingleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(itemNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(currPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(endTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(maxPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(jLabel10)
                    .addComponent(statusField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(24, 24, 24)
                .addGroup(singleItemDisplayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startSingleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(suspendSingleItem)
                    .addComponent(closeSingleItem))
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.CardLayout());

        signInButton.setText("Sign in");
        signInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                signInButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Bitstream Vera Sans", 0, 24));
        jLabel1.setText("ESNIPE 2  Bidding Agent");

        jLabel2.setText("We bid for you on ebay by smart agent!");

        userPassLabel.setText("Password");

        userIDLabel.setText("User ID");

        userIDField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userIDFieldActionPerformed(evt);
            }
        });

        userPassField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userPassFieldActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(146, 146, 146)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userIDLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(userPassLabel))
                .addGap(101, 101, 101)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(userPassField)
                    .addComponent(userIDField, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                .addContainerGap(193, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userIDLabel)
                    .addComponent(userIDField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userPassLabel)
                    .addComponent(userPassField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(70, 70, 70))
        );

        javax.swing.GroupLayout SignInPanelLayout = new javax.swing.GroupLayout(SignInPanel);
        SignInPanel.setLayout(SignInPanelLayout);
        SignInPanelLayout.setHorizontalGroup(
            SignInPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SignInPanelLayout.createSequentialGroup()
                .addGap(236, 236, 236)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(292, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SignInPanelLayout.createSequentialGroup()
                .addContainerGap(337, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(216, 216, 216))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, SignInPanelLayout.createSequentialGroup()
                .addContainerGap(642, Short.MAX_VALUE)
                .addComponent(signInButton, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60))
            .addGroup(SignInPanelLayout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
        SignInPanelLayout.setVerticalGroup(
            SignInPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SignInPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jLabel2)
                .addGap(56, 56, 56)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(signInButton)
                .addContainerGap(175, Short.MAX_VALUE))
        );

        getContentPane().add(SignInPanel, "card1");

        jLabel3.setText("Please enter the URLs for the items you want to bid as well as the maximum price you would like to pay for them:");

        jLabel4.setText("URL");

        addItemButton.setText("Add");
        addItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemButtonActionPerformed(evt);
            }
        });

        maxPriceField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maxPriceFieldActionPerformed(evt);
            }
        });

        jLabel13.setText("End Time");

        closeTimeField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeTimeFieldActionPerformed(evt);
            }
        });

        start1Button.setText("start");
        start1Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                start1ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(maxPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(start1Button, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(342, 342, 342))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxPriceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(closeTimeField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(start1Button))
                .addContainerGap())
        );

        jLabel5.setText("Set Price");

        javax.swing.GroupLayout inputPanelLayout = new javax.swing.GroupLayout(inputPanel);
        inputPanel.setLayout(inputPanelLayout);
        inputPanelLayout.setHorizontalGroup(
            inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inputPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(inputPanelLayout.createSequentialGroup()
                        .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(inputPanelLayout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(24, 24, 24)
                                .addComponent(URLField, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(addItemButton, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(54, 54, 54))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inputPanelLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        inputPanelLayout.setVerticalGroup(
            inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(inputPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addGap(16, 16, 16)
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(URLField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addItemButton)
                    .addComponent(jLabel4))
                .addGroup(inputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(inputPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inputPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addGap(20, 20, 20))))
        );

        displayTable.setModel(model);
        displayTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(displayTable);

        closeSingleButton.setText("close");
        closeSingleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeSingleButtonActionPerformed(evt);
            }
        });

        resumeSingleButton.setBackground(new java.awt.Color(0, 255, 0));
        resumeSingleButton.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        resumeSingleButton.setForeground(new java.awt.Color(255, 255, 255));
        resumeSingleButton.setText("resume");
        resumeSingleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resumeSingleButtonActionPerformed(evt);
            }
        });

        suspendSingleButton.setBackground(new java.awt.Color(255, 0, 0));
        suspendSingleButton.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        suspendSingleButton.setForeground(new java.awt.Color(255, 255, 255));
        suspendSingleButton.setText("suspend");
        suspendSingleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                suspendSingleButtonActionPerformed(evt);
            }
        });

        retrySingleButton.setText("Retry ");
        retrySingleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                retrySingleButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(resumeSingleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(suspendSingleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeSingleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(retrySingleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(401, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeSingleButton)
                    .addComponent(retrySingleButton)
                    .addComponent(resumeSingleButton)
                    .addComponent(suspendSingleButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        SuspendAllButton.setText("Suspend All");
        SuspendAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SuspendAllButtonActionPerformed(evt);
            }
        });

        closeAllButton.setText("Close All");
        closeAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeAllButtonActionPerformed(evt);
            }
        });

        ResumeAllButton.setText("Resume All");
        ResumeAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResumeAllButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(ResumeAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(SuspendAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(closeAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(391, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ResumeAllButton)
                    .addComponent(SuspendAllButton)
                    .addComponent(closeAllButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        quitButton.setText("Log out");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout biddingPanelLayout = new javax.swing.GroupLayout(biddingPanel);
        biddingPanel.setLayout(biddingPanelLayout);
        biddingPanelLayout.setHorizontalGroup(
            biddingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, biddingPanelLayout.createSequentialGroup()
                .addGroup(biddingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, biddingPanelLayout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 916, Short.MAX_VALUE))
                    .addGroup(biddingPanelLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(inputPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(biddingPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(quitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, biddingPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        biddingPanelLayout.setVerticalGroup(
            biddingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(biddingPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(inputPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(biddingPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(quitButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        biddingTabPanel.addTab("tab1", biddingPanel);

        getContentPane().add(biddingTabPanel, "card2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void signInButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signInButtonActionPerformed
       boolean success = ebayAPI.startSession(userIDField.getText(), String.valueOf(userPassField.getPassword()));
       // boolean success = true;
        if(success){
            cardLayout.show(getContentPane(), "card2");
        }else{
            jOptionPane1.showMessageDialog(frame, "Failed to login. Please enter your username or password again");
            userIDField.setText("");
            userPassField.setText("");
        }
            
        
}//GEN-LAST:event_signInButtonActionPerformed

    private void userIDFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userIDFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_userIDFieldActionPerformed

    private void addItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemButtonActionPerformed
        this.currURL = this.URLField.getText();
        if(EbayItem.getItemNo(this.currURL) == null) {
            jOptionPane1.showMessageDialog(frame, "Please input a valid URL of an auction page.");
            this.URLField.setText("");
            return;
        }
        if(URLs.contains(this.currURL)){
            jOptionPane1.showMessageDialog(frame, "You can not enter the same URL twice! ");
            this.URLField.setText("");
        }
        else{
        URLs.add(this.currURL);
        model.insertRow(model.getRowCount(),new Object[]{this.currURL,"NULL","NULL","NULL","NULL","NULL","NULL","NULL",});
        displayTable.changeSelection(model.getRowCount()-1, 0, false, false);
        this.currItem = new EbayItem(URLField.getText(),ebayAPI);
        this.currRunnable = new ItemRunnable(this.currItem,ebayAPI,this);
        Thread t = new Thread(this.currRunnable);
        t.start();
     

        System.out.println("add Item: current Id"+this.currID);
        
        this.items.add(this.currRunnable);
        System.out.println("current row number: "+ model.getRowCount());
        }

    }//GEN-LAST:event_addItemButtonActionPerformed

    private void closeTimeFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeTimeFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_closeTimeFieldActionPerformed

    private void suspendSingleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suspendSingleButtonActionPerformed
        int row = this.displayTable.getSelectedRow();
        this.items.get(row).suspend();

}//GEN-LAST:event_suspendSingleButtonActionPerformed

    private void resumeSingleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resumeSingleButtonActionPerformed
        int row = this.displayTable.getSelectedRow();
        this.items.get(row).resume();
}//GEN-LAST:event_resumeSingleButtonActionPerformed

    private void closeSingleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeSingleButtonActionPerformed
        int row = this.displayTable.getSelectedRow();
        if(row ==-1){
            jOptionPane1.showMessageDialog(frame, "Please select a row.");
        }else{
        System.out.println(" row : "+row);
        this.items.get(row).close(); //close the item runnable
        System.out.println(" after items.close");
        this.items.remove(row);//remove the runnable from vector list.
        this.IDs.remove(row);
        this.URLs.remove(row);
        this.model.removeRow(row); //remove the row from table.
        System.out.println("single close end");
        }
    }//GEN-LAST:event_closeSingleButtonActionPerformed

    private void maxPriceFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maxPriceFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_maxPriceFieldActionPerformed

    private void start1ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_start1ButtonActionPerformed
        int row = this.displayTable.getSelectedRow();
        if(row==-1){
            jOptionPane1.showMessageDialog(frame, "Please select a row.");
        }

        else{
            System.out.println("row = "+row);
            System.out.println("items.rowCount = "+items.size());
        this.items.get(row).setEndTime(EbayAPIImpl.parseCal(this.closeTimeField.getText()));
        this.items.get(row).setUserPrice(Double.parseDouble(this.maxPriceField.getText()));
        this.items.get(row).resume();
         System.out.println("Start button: endTime: "+ this.closeTimeField.getText());
        //displayTable.setValueAt(this.closeTimeField.getText(), row,6);
        //displayTable.setValueAt(this.maxPriceField.getText(), row,4);
        this.closeTimeField.setText("");
        this.maxPriceField.setText("");
        }
    }//GEN-LAST:event_start1ButtonActionPerformed

    private void SuspendAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SuspendAllButtonActionPerformed
        for (int i=0; i<items.size(); i++){
            items.get(i).suspend();
        }
    }//GEN-LAST:event_SuspendAllButtonActionPerformed

    private void closeAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeAllButtonActionPerformed
        for(int i = 0; i<items.size();i++){
            items.get(i).close();
        }
        items.clear();
        URLs.clear();
        while(model.getRowCount()>0){
            model.removeRow(0);
        }

      
    }//GEN-LAST:event_closeAllButtonActionPerformed

    private void retrySingleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_retrySingleButtonActionPerformed
        int row = this.displayTable.getSelectedRow();
        if(row==-1){
            jOptionPane1.showMessageDialog(frame, "Please select a row.");
        }
        items.get(row).retry();

}//GEN-LAST:event_retrySingleButtonActionPerformed

    private void ResumeAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResumeAllButtonActionPerformed
        for(int i = 0; i<items.size(); i++){
            items.get(i).resume();
        }
    }//GEN-LAST:event_ResumeAllButtonActionPerformed

    private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitButtonActionPerformed
        System.exit(0);// TODO add your handling code here:
}//GEN-LAST:event_quitButtonActionPerformed

    private void userPassFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userPassFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_userPassFieldActionPerformed

    public String getItemDisplayText(String URL,double ID, String name, double curPrice, String status, double endTime, double lastTime){
        String text = "URL: " +URL + "\n" +"Name: "+name+"\n"+ "Current Price: "+curPrice
                +"   Max Price: "+ _maxPrice +"Status: "+status+"   End Time: "+endTime+"   Last Time: "+lastTime+"\n";

        return text;

    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Esnipe2Gui().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ResumeAllButton;
    private javax.swing.JPanel SignInPanel;
    private javax.swing.JButton SuspendAllButton;
    private javax.swing.JTextField URLField;
    private javax.swing.JTextField URLSingleField;
    private javax.swing.JButton addItemButton;
    private javax.swing.JPanel biddingPanel;
    private javax.swing.JTabbedPane biddingTabPanel;
    private javax.swing.JButton closeAllButton;
    private javax.swing.JButton closeSingleButton;
    private javax.swing.JButton closeSingleItem;
    private javax.swing.JTextField closeTimeField;
    private javax.swing.JTextField currPriceField;
    private javax.swing.JTable displayTable;
    private javax.swing.JTextField endTimeField;
    private javax.swing.JPanel inputPanel;
    private javax.swing.JTextField itemNameField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JOptionPane jOptionPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField lastTimeField;
    private javax.swing.JTextField maxPrice;
    private javax.swing.JTextField maxPriceField;
    private javax.swing.JButton quitButton;
    private javax.swing.JButton resumeSingleButton;
    private javax.swing.JButton retrySingleButton;
    private javax.swing.JButton signInButton;
    private javax.swing.JPanel singleItemDisplayPanel;
    private javax.swing.JButton start1Button;
    private javax.swing.JButton startSingleButton;
    private javax.swing.JTextField statusField;
    private javax.swing.JButton suspendSingleButton;
    private javax.swing.JButton suspendSingleItem;
    private javax.swing.JTextField userIDField;
    private javax.swing.JLabel userIDLabel;
    private javax.swing.JPasswordField userPassField;
    private javax.swing.JLabel userPassLabel;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel model;
    CardLayout cardLayout;
    @Override
    public void updateID(String url, String id) {
        this.currID = id;
        IDs.add(currID);
        int row = model.getRowCount();
        displayTable.setValueAt(this.currID, row-1, 1);
       
        

    }

    @Override
    public void updateName(String id, String name) {
        this.currName = name;
        int row = IDs.indexOf(id);
        if(row>-1&&row<model.getRowCount()){
        displayTable.setValueAt(name, row,2);
        }
    }

    @Override
    public void updateCurPrice(String id, Pricepair price) {
        this.currPrice = price.toString();
        int row = IDs.indexOf(id);
        if(row>-1&&row<model.getRowCount()){
        displayTable.setValueAt(price, row, 3);
        }
    }

    @Override
    public void updateUserPrice(String id, double price) {
        this.currMaxPrice = Double.toString(price);
        int row = IDs.indexOf(id);
        if(row>-1&&row<model.getRowCount()){
        displayTable.setValueAt(price, row, 4);
        }
    }

    @Override
    public void updateStatus(String id, ItemStatus status) {
        int startIdx = status.toString().indexOf("_")+1;
        this.currStatus = status.toString().substring(startIdx);
        int row = IDs.indexOf(id);
        if(row>-1&&row<model.getRowCount()){
        displayTable.setValueAt(this.currStatus, row, 5);
              if(this.currStatus.equals(ItemStatus.STATUS_WON.toString().substring(startIdx))){
            for (int i=0;i<items.size(); i++){
                items.get(i).suspend();
           }
        }
        }
    }

    @Override
    public void updateEndTime(String id, Calendar endtime) {
        this.currEndTime = EbayAPIImpl.parseCalInv(endtime);
        int row = IDs.indexOf(id);
        if(row>-1&&row<model.getRowCount()){
        displayTable.setValueAt(this.currEndTime, row,6);
        }
    }

    @Override
    public void updateLastTime(String id, Calendar lastupdate) {
        this.currLastTime = EbayAPIImpl.parseCalInv(lastupdate);
        int row = IDs.indexOf(id);
        if(row>-1&&row<model.getRowCount()){
        displayTable.setValueAt(this.currLastTime, row, 7);
        }
    }


}
